package com.yan.club.handler;

import com.yan.club.exception.ServiceException;
import com.yan.club.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.yan.club.result.ResultCode.INTERNAL_SERVER_ERROR;

/**
 * @author 彦
 * @since 2024/2/2 10:48
 */
@RestControllerAdvice
@Log4j2
public class GlobalExceptionHandler {

    /**
     * 业务异常
     */
    @ExceptionHandler(ServiceException.class)
    public Result<String> handleServiceException(ServiceException e) {
        log.error(e.getMessage(), e);
        Integer code = e.getCode();
        return code != null ?
                Result.error(code, e.getMessage()) : Result.error(INTERNAL_SERVER_ERROR.code, e.getMessage());
    }

}
